package com.allinpay.bizroute.proto;

import com.allinpay.bizroute.genproto.ChkRouteSvcGrpc;
import com.allinpay.bizroute.genproto.MerchantTerm;
import com.allinpay.bizroute.genproto.RouteDetail;
import com.allinpay.bizroute.genproto.RouteRule;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @ClassName ChkRouteSvc
 * @Description ChkRouteSvc
 * @Author richard
 * @Date 2021/12/15 5:44 PM
 */
@Slf4j
@Component
public class ChkRouteSvc  extends ChkRouteSvcGrpc.ChkRouteSvcImplBase {

    @Override
    public void chkByMchtTerm(MerchantTerm request, StreamObserver<RouteRule> responseObserver) {
        // TODO: 2021/12/15  后期如何做对象映射？
        String mcd = request.getMcd();
        String tcd = request.getTcd();
        log.info("proc chkByMchtTerm,mcd:{}, tcd:{} " ,mcd, tcd);

        RouteRule routeRule = chkRoute(mcd, tcd);

        //设置返回结果
        responseObserver.onNext(routeRule);

        //返回
        responseObserver.onCompleted();
    }

    private RouteRule chkRoute(String mcd, String tcd){

        Random random = new Random();
        RouteDetail routeDetail = RouteDetail.newBuilder().setName("ChkRouteSvcImpl")
                .setNum(random.nextInt(10)).build();

        RouteRule routeRule = RouteRule.newBuilder().setRspCd(200)
                .setDetail(routeDetail)
                .build();

        return routeRule;
    }
}
